package com.ruoyi.volunteer.activity.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.volunteer.activity.mapper.VoluntaryActivityMapper;
import com.ruoyi.volunteer.activity.domain.VoluntaryActivity;
import com.ruoyi.volunteer.activity.service.IVoluntaryActivityService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 活动管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class VoluntaryActivityServiceImpl implements IVoluntaryActivityService 
{
    @Resource
    private VoluntaryActivityMapper voluntaryActivityMapper;

    /**
     * 查询活动管理
     * 
     * @param id 活动管理主键
     * @return 活动管理
     */
    @Override
    public VoluntaryActivity selectVoluntaryActivityById(Long id)
    {
        return voluntaryActivityMapper.selectVoluntaryActivityById(id);
    }

    /**
     * 查询活动管理列表
     * 
     * @param voluntaryActivity 活动管理
     * @return 活动管理
     */
    @Override
    public List<VoluntaryActivity> selectVoluntaryActivityList(VoluntaryActivity voluntaryActivity)
    {
        List<VoluntaryActivity> list = voluntaryActivityMapper.selectVoluntaryActivityList(voluntaryActivity);
        LoginUser loginUser = getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        for (SysRole role : roles) {
            if (role.getRoleId() == 101) {
                List<VoluntaryActivity> list2 = new ArrayList<>();
                for (VoluntaryActivity item : list) {
                    if (item.getEnd().after(new Date())) {
                        list2.add(item);
                    }
                }
                System.out.println("aaaaaaaaaaaaa");
                return list2;
            } else if (role.getRoleId() == 102) {
                List<VoluntaryActivity> list2 = new ArrayList<>();
                for (VoluntaryActivity item : list) {
                    if (item.getUserId().equals(loginUser.getUser().getUserId())) {
                        list2.add(item);
                    }
                }
                System.out.println("bbbbbbbbbbbbbb");
                return list2;
            }
        }
        return list;
    }

    /**
     * 新增活动管理
     * 
     * @param voluntaryActivity 活动管理
     * @return 结果
     */
    @Override
    public int insertVoluntaryActivity(VoluntaryActivity voluntaryActivity)
    {
        return voluntaryActivityMapper.insertVoluntaryActivity(voluntaryActivity);
    }

    /**
     * 修改活动管理
     * 
     * @param voluntaryActivity 活动管理
     * @return 结果
     */
    @Override
    public int updateVoluntaryActivity(VoluntaryActivity voluntaryActivity)
    {
        return voluntaryActivityMapper.updateVoluntaryActivity(voluntaryActivity);
    }

    /**
     * 批量删除活动管理
     * 
     * @param ids 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public int deleteVoluntaryActivityByIds(Long[] ids)
    {
        return voluntaryActivityMapper.deleteVoluntaryActivityByIds(ids);
    }

    /**
     * 删除活动管理信息
     * 
     * @param id 活动管理主键
     * @return 结果
     */
    @Override
    public int deleteVoluntaryActivityById(Long id)
    {
        return voluntaryActivityMapper.deleteVoluntaryActivityById(id);
    }
}
